package team.se.tms.controller.material;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import team.se.tms.annotation.SpecialCheck;
import team.se.tms.dto.CourseMaterialFile;
import team.se.tms.dto.Page;
import team.se.tms.exception.InsertException;
import team.se.tms.exception.ParamException;
import team.se.tms.po.CourseMaterialInfo;
import team.se.tms.po.TeachCourseInfo;
import team.se.tms.po.TeachCourse_CourseMaterialInfo;
import team.se.tms.service.CourseMaterialInfoService;
import team.se.tms.service.TeachCourseInfoService;
import team.se.tms.util.ResultUtil;

import javax.annotation.Resource;
import java.util.List;

/**
 * 课程资料信息控制层
 *
 * @author winkyle
 * @date 2018/11/27
 */
@Slf4j
@Controller
@RequestMapping(value="/material")
public class CourseMaterialInfoController {

    @Autowired
    private CourseMaterialInfoService courseMaterialInfoService;
    @Autowired
    private TeachCourseInfoService teachCourseInfoService;

    /**
     * 根据授课id获取课程资料
     *
     * @param teachCourseId 授课id
     * @param page 分页
     * @return json字符串
     * @throws Exception 异常
     * @create on 2018/12/5 by winkyle
     */
    @RequestMapping(value="/search_course_material_files")
    @ResponseBody
    public Object searchCourseMaterialFiles(@SpecialCheck("Pk")String teachCourseId, Page page) throws Exception {
        log.debug("=======================searchCourseMaterialFiles");
        return page.getDataTabelJson(courseMaterialInfoService.listCourseMaterialFiles(teachCourseId, page));
    }

    /**
     * 上传课程资料
     *
     * @param teachCourseId 授课id
     * @param multipartFile 文件
     * @return json字符串
     * @throws Exception 异常
     * @create on 2018/12/5 by winkyle
     */
    @RequestMapping(value="/upload_course_material")
    @ResponseBody
    public Object uploadCourseMaterial(@SpecialCheck("Pk")String teachCourseId, @RequestParam("file")MultipartFile multipartFile) throws Exception {
        if(multipartFile==null){
            throw new ParamException("文件不能为空");
        }
        log.debug("上传了{},类型{}", multipartFile.getOriginalFilename(),multipartFile.getContentType());
        courseMaterialInfoService.saveCourseMaterial(teachCourseId,multipartFile);
        return ResultUtil.success("上传成功");
    }



    /**
     * 删除课程资料
     *
     * @param teachCourseId 授课id
     * @param courseMaterialIds 课程资料id
     * @param deleteFile 是否删除文件
     * @return json字符串
     * @throws Exception 异常
     * @create on 2018/12/5 by winkyle
     */
    @RequestMapping(value="/drop_course_materials")
    @ResponseBody
    public Object dropCourseMaterials(@SpecialCheck("Pk")String teachCourseId, @SpecialCheck("Pk")@RequestParam("courseMaterialIds")List<String> courseMaterialIds, Boolean deleteFile) throws Exception {
        log.debug("teachCourseId======{}",teachCourseId);
        log.debug("dropCourseMaterials======{}",courseMaterialIds==null?-1:courseMaterialIds.toString());
        log.debug("deleteFile======{}",deleteFile);
        courseMaterialInfoService.removeCourseMaterials(teachCourseId,courseMaterialIds,deleteFile);
        return ResultUtil.success("删除成功");
    }

    /**
     * 查询该用户所有授课
     * @create on 2018/12/28 by argentum
     *
     * @param userId 用户id
     * @return
     */
    @RequestMapping("listTeachCourseByUserId")
    public Object searchTeachCourse(String userId) throws Exception{
        List<TeachCourseInfo> teachCourseInfos = teachCourseInfoService.listTeachCourseInfos(userId);
        ModelAndView mv = new ModelAndView();
        mv.addObject("teachCourseInfos",teachCourseInfos);
        mv.setViewName("course_material/teach_course");//待定
        return mv;
    }

    /**
     * 云端上传
     * @create on 2018/12/28 by argentum
     *
     * @param teachCourseCourseMaterialInfos
     * @return
     * @throws Exception
     */
    @RequestMapping("cloudUpload")
    public Object cloudUploadCourseMaterials(List<TeachCourse_CourseMaterialInfo> teachCourseCourseMaterialInfos) throws Exception{
        Boolean success = courseMaterialInfoService.saveTeachCourse_CourseMaterialInfos(teachCourseCourseMaterialInfos);
        if(!success){
            throw new InsertException();
        }
        return ResultUtil.success("上传成功");
    }

    /**
     * 课程资料页
     * @create on 2018/12/31 by argentum
     *
     * @return
     * @throws Exception
     */
    @RequestMapping("toCourseMaterialList")
    public String toCourseMaterialList() throws Exception{
        return "course_material/course_material_list";
    }

    /**
     * 获取课程资料
     *
     * @param page 分页
     * @return json字符串
     * @throws Exception 异常
     * @create on 2018/12/5 by winkyle
     */
    @RequestMapping(value="/search_all_course_material_files")
    @ResponseBody
    public Object searchAllCourseMaterialFiles(Page page) throws Exception {
        return page.getDataTabelJson(courseMaterialInfoService.listCourseMaterialFiles(page));
    }

}
